/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import controllers.Security;
import play.db.jpa.Model;

/**
 * 
 * @author Ousama
 */
@Entity
public class ActionItem extends Model {

    public Date startDate;
    public Date endDate;
    public String title;
    public String body;
    public String itemStartDate;
    public String itemEndDate;
    @ManyToMany(mappedBy = "actionItem", cascade = CascadeType.PERSIST)
    // ERROR
    public List<IdeaDeveloper> volunteers = new ArrayList<IdeaDeveloper>();
    @ManyToOne
    public ActionPlan actionPlan;
    @ManyToMany(mappedBy = "managedActionItem", cascade = CascadeType.PERSIST)
    public List<Organizer> managers = new ArrayList<Organizer>();
    @ManyToMany
    public List<Tag> tags = new ArrayList<Tag>();
    @ManyToMany(mappedBy = "actionItemTop", cascade = CascadeType.PERSIST)
    public List<Topic> topics = new ArrayList<Topic>();
    @ManyToMany(mappedBy = "actionItemEnt", cascade = CascadeType.PERSIST)
    public List<Entities> entities = new ArrayList<Entities>();
    @ManyToMany(mappedBy = "actionItemIdea", cascade = CascadeType.PERSIST)
    public List<Idea> ideas = new ArrayList<Idea>();
    @OneToMany(mappedBy = "item", cascade = CascadeType.ALL)
    public List<Request> requests = new ArrayList<Request>();

    /**
     * Empty Constructor for initializing the Action Item
     * @author
     */
    public ActionItem() {
    }

    public ActionItem(String title, String body, Date startDate, Date endDate) {
        this.title = title;
        this.body = body;
        this.startDate = startDate;
        this.endDate = endDate;
        String start = startDate.toString();
        this.itemStartDate = start.substring(4, 10) + " " + start.substring(start.length() - 4, start.length());
        String end = endDate.toString();
        this.itemEndDate = end.substring(4, 10) + " " + end.substring(end.length() - 4, end.length());
    }

    public void delete2(long doerId) {
        // deleting the requests
        IdeaDeveloper ideaDev = IdeaDeveloper.findById(doerId);
        while (!requests.isEmpty()) {
            Notification.notify(" has deleted " + this.title + " ",
                    requests.get(0).dev, ideaDev, new Date());
            requests.get(0).delete2();
        }
        // deletes it from the topics
        for (int i = 0; i < topics.size(); i++) {
            topics.get(i).actionItemTop.remove(this);
            topics.get(i).save();
        }
        // deletes it from the ideas
        for (int i = 0; i < ideas.size(); i++) {
            ideas.get(i).actionItemIdea.remove(this);
            ideas.get(i).save();
        }
        // delete the tag relation
        for (int i = 0; i < tags.size(); i++) {
            for (IdeaDeveloper follower : tags.get(i).followed) {
                Notification.notify(" has deleted " + this.title + " ",
                        follower, ideaDev, new Date());
            }
            tags.get(i).actionitems.remove(this);
            tags.get(i).save();
        }
        // no managers anymore
        for (int i = 0; i < managers.size(); i++) {
            if (managers.get(i).ideaDeveloper.id != doerId) {
                Notification.notify(" has deleted " + this.title + " ",
                        managers.get(i).ideaDeveloper, ideaDev, new Date());
            }
            managers.get(i).managedActionItem.remove(this);
            managers.get(i).save();
        }
        // no voluners anymore
        for (int i = 0; i < volunteers.size(); i++) {
            Notification.notify(" has deleted " + this.title + " ",
                    volunteers.get(i), ideaDev, new Date());
            volunteers.get(i).actionItem.remove(this);
            volunteers.get(i).save();
        }

        /**
         * Helper to delete the action item and all its components
         * @author
         */
        // removes the actionPlan
        actionPlan.actionItems.remove(this);
        actionPlan.save();
        this.delete();
    }

    /**
     * Checks if the given tag is already a tag of this idea and invokes the
     * method addTag() if it exits the while loop without returning true which
     * means the tag does not exist it is clear to go ahead and add the tag to
     * this idea
     *
     * @author Zein
     * @param String
     *            name of the tag to be added
     * @return Boolean, true if tag already exists, false otherwise
     */
    public boolean existingTag(String name) {
        int i = 0;
        while (i < this.tags.size()) {
            if (this.tags.get(i).name.equalsIgnoreCase(name)) {
                return true;
            }
            i++;
        }

        return false;
    }

    /**
     * Relates the given tag to this Idea and it is invoked from the
     * existingTag() method
     *
     * @author Zein
     * @param String
     *            name of the tag to be added
     */
    public void addTag(String name) {
        Tag thistag;
        List<Tag> tagActionItem = Tag.find("name = ?", name).fetch();
        if (tagActionItem.isEmpty()) {
            Tag newtag = new Tag(name);
            this.tags.add(newtag);
            newtag.addActionItem(this);
            thistag = newtag;
        } else {
            this.tags.add(tagActionItem.get(0));
            tagActionItem.get(0).addActionItem(this);
            thistag = tagActionItem.get(0);
        }
        this.save();

        Log log = new Log("^ was added to ^.");
        log.ids = thistag.id + "," + this.id;
        log.types = "tag,actionitem";
        log.save();
    }

    /**
     * Removes the tag from the idea's tags list and removes the idea from the
     * tag's ideas list.
     *
     * @author Zein
     * @param String
     *            name of the tag to be removed
     */
    public void removeTag(String name) {
        Tag thistag = null;
        int i = 0;
        while (i < this.tags.size()) {
            if (this.tags.get(i).name.equalsIgnoreCase(name)) {
                thistag = this.tags.get(i);
                this.tags.get(i).removeActionItem(this);
                this.tags.remove(this.tags.get(i));
            }
            i++;
        }
        Log log = new Log("^ was deleted from ^.");
        log.ids = thistag.id + "," + this.id;
        log.types = "tag,actionitem";
        log.save();

        this.save();


    }
}
